﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace baseClass.controls
{
    public partial class emPrivate : common.control.baseUserControl
    {
        public emPrivate()
        {
            InitializeComponent();
            SetMaxLength();
        }
        private void SetMaxLength()
        {
            data.baseDS.employeeDataTable tbl = new data.baseDS.employeeDataTable();
            expertiseCb.maxLen = tbl.expertiseColumn.MaxLength;
            dobEd.MaxLength = tbl.dobColumn.MaxLength;
            placeOfOriginEd.MaxLength = tbl.placeOfOriginColumn.MaxLength;
            eduAreaEd.MaxLength = tbl.eduAreaColumn.MaxLength;

            expertiseCb.minSelectedItems = 1;
        }

        public virtual void Init()
        {
            nationalityCb.LoadData();
            ethnicCb.LoadData();
            religionCb.LoadData();
            maritalStatCb.LoadData();
            bloodTypeCb.LoadData();
            eduLeveCb.LoadData();
            comUnionStatCb.LoadData();
            comPartyStatCb.LoadData();
            expertiseCb.LoadData();
        }
        public virtual void SetData(data.baseDS.employeeRow row)
        {
            dobEd.myDateTime = row.dob;
            pobEd.Text = row.pob;
            placeOfOriginEd.Text = (row.IsplaceOfOriginNull() ? "" : row.placeOfOrigin);

            nationalityCb.myValue = (row.Isnationality1Null() ? null : row.nationality1);
            ethnicCb.myValue = (row.IsethnicNull() ? null : row.ethnic);
            religionCb.myValue = (row.IsreligionNull() ? null : row.religion);
            maritalStatCb.myValue = (row.IsmaritalStatusNull() ? application.myTypes.maritalStatus.None : (application.myTypes.maritalStatus)row.maritalStatus);

            eduLeveCb.myValue = (row.IseduLevelNull() ? null : row.eduLevel);
            eduAreaEd.Text = (row.IseduAreaNull() ? null: row.eduArea);

            bloodTypeCb.myValue = (row.IsbloodGroupNull() ? application.myTypes.bloodType.None : (application.myTypes.bloodType)row.bloodGroup);

            if (row.IscomPartyJoinDateNull())
            {
                comPartyChk.Checked = false;
                comPartyDateEd.myDateTime = common.Consts.constNullDate;
            }
            else
            {
                comPartyChk.Checked = true;
                comPartyDateEd.myDateTime = row.comPartyJoinDate;
            }
            comPartyStatCb.myValue = (row.IscomPartyStatusNull() ? null : row.comPartyStatus);

            if (row.IscomUnionJoinDateNull())
            {
                comUnionChk.Checked = false;
                comUnionDateEd.myDateTime = common.Consts.constNullDate;
            }
            else
            {
                comUnionChk.Checked = true;
                comUnionDateEd.myDateTime = row.comUnionJoinDate;
            }
            comUnionStatCb.myValue = (row.IscomUnionStatusNull() ? null : row.comUnionStatus);

            expertiseCb.myItemString = (row.IsexpertiseNull()?"" : row.expertise);
        }
        public virtual void GetData(data.baseDS.employeeRow row)
        {
            row.dob = dobEd.myDateTime;
            row.pob = pobEd.Text.Trim();
            if (placeOfOriginEd.Text.Trim() == "") row.SetplaceOfOriginNull(); else row.placeOfOrigin = placeOfOriginEd.Text.Trim();
            
            if (nationalityCb.myValue.Trim() == "") row.Setnationality1Null(); else row.nationality1 = nationalityCb.myValue.Trim();
            if (ethnicCb.myValue.Trim() == "") row.SetethnicNull(); else row.ethnic = nationalityCb.myValue.Trim();
            if (religionCb.myValue.Trim() == "") row.SetreligionNull(); else row.religion = religionCb.myValue.Trim();
            row.maritalStatus = (byte)maritalStatCb.myValue;

            if (eduLeveCb.myValue.Trim() == "") row.SeteduLevelNull(); else row.eduLevel = eduLeveCb.myValue.Trim();
            if (eduAreaEd.Text.Trim() == "") row.SeteduAreaNull(); else row.eduLevel = eduAreaEd.Text.Trim();
            
            row.bloodGroup = (byte)bloodTypeCb.myValue;

            if (comPartyChk.Checked)  row.comPartyJoinDate = comPartyDateEd.myDateTime;
            else row.SetcomPartyJoinDateNull();
            if (comPartyStatCb.myValue.Trim() == "") row.SetcomPartyStatusNull(); else row.comPartyStatus = comPartyStatCb.myValue.Trim();

            if (comUnionChk.Checked) row.comUnionJoinDate = comUnionDateEd.myDateTime;
            else row.SetcomUnionJoinDateNull();
            if (comUnionStatCb.myValue.Trim() == "") row.SetcomUnionStatusNull(); else row.comUnionStatus = comUnionStatCb.myValue.Trim();

            if (expertiseCb.myItemString.Trim() == "") row.SetexpertiseNull(); else row.expertise = expertiseCb.myItemString.Trim();
        }

        public virtual bool CheckData()
        {
            bool retVal = true;
            ClearNotifyError();
            if (dobEd.myDateTime == common.Consts.constNullDate)
            {
                NotifyError(dobLbl);
                retVal = false;
            }
            if (pobEd.Text.Trim() == "")
            {
                NotifyError(pobLbl);
                retVal = false;
            }

            if (comPartyChk.Checked)
            {
                if (comPartyDateEd.myDateTime == common.Consts.constNullDate)
                {
                    NotifyError(comPartyDateLbl);
                    retVal = false;
                }
            }
            if (comUnionChk.Checked)
            {
                if (comUnionDateEd.myDateTime == common.Consts.constNullDate)
                {
                    NotifyError(comUnionDateLbl);
                    retVal = false;
                }
            }

            if (!expertiseCb.DataValid(false))
            {
                NotifyError(expertiseLbl);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            dobEd.ReadOnly = state;
            pobEd.ReadOnly = state;
            placeOfOriginEd.ReadOnly = state;

            nationalityCb.Enabled = !state;
            ethnicCb.Enabled = !state;
            religionCb.Enabled = !state;
            maritalStatCb.Enabled = !state;

            eduLeveCb.Enabled = !state;
            eduAreaEd.ReadOnly = state;

            bloodTypeCb.Enabled = !state;

            comPartyGb.Enabled = !state;
            comUnionGb.Enabled = !state;
            expertiseCb.Enabled = !state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            dobEd.Focus();
        }
    }
}
